<?php

namespace app\api\controller;

use app\BaseController;
use EasyWeChat\Work\Application;
use think\facade\Log;

class Test extends BaseController
{
    protected $wework;

    protected function initialize()
    {
        $config = [
            'corp_id' => 'ww255caf594d19f8ee',
            'secret' => 'v_rguWjB4Lf-xJukUfT-XkFDHZIUCIXDal2eBy0k-zo',
            'token' => 'aAWX26Mp5VBKOyn',
            'aes_key' => 'tdKYzq8OVoC7vDfghWcGbmE1AjLMiP2BaIX049QJrRZ',

            /**
             * 接口请求相关配置，超时时间等，具体可用参数请参考：
             * https://github.com/symfony/symfony/blob/5.3/src/Symfony/Contracts/HttpClient/HttpClientInterface.php
             */
            'http' => [
                'throw' => true, // 状态码非 200、300 时是否抛出异常，默认为开启
                'timeout' => 5.0,
                // 'base_uri' => 'https://qyapi.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用，根据不同的模块配置不同的 uri

                'retry' => true, // 使用默认重试配置
                //  'retry' => [
                //      // 仅以下状态码重试
                //      'status_codes' => [429, 500]
                //       // 最大重试次数
                //      'max_retries' => 3,
                //      // 请求间隔 (毫秒)
                //      'delay' => 1000,
                //      // 如果设置，每次重试的等待时间都会增加这个系数
                //      // (例如. 首次:1000ms; 第二次: 3 * 1000ms; etc.)
                //      'multiplier' => 3
                //  ],
            ],
        ];
        $this->wework = new Application($config);
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    public function getToken()
    {
        $accessToken = $this->wework->getAccessToken();
        $accessToken->getToken(); // string
        response(200, '操作成功', ['token' => $accessToken->getToken()]);
    }

    public function msg()
    {
        Log::channel('wechat')->write("param：" . json_encode($this->request->param(), JSON_UNESCAPED_UNICODE));
        $server = $this->wework->getServer();
        $message = $server->getRequestMessage();
        $messageDecrypted = $server->getDecryptedMessage();
        Log::channel('wechat')->write("原始消息：" . json_encode($message, JSON_UNESCAPED_UNICODE) . "解密消息：" . json_encode($messageDecrypted, JSON_UNESCAPED_UNICODE));
    }

    public function syncCallProgram()
    {
        $post = $this->request->post(['program_id', 'ability_id', 'notify_id', 'request_data']);
        $response = $this->wework->getClient()->postJson('/cgi-bin/chatdata/sync_call_program', $post);
        response(200, '操作成功', $response->toArray());
    }

    public function openDebugMode()
    {
        $post = $this->request->post(['program_id', 'debug_token']);
        $response = $this->wework->getClient()->postJson('/cgi-bin/chatdata/open_debug_mode', $post);
        response(200, '操作成功', $response->toArray());
    }

    public function closeDebugMode()
    {
        $post = $this->request->post(['program_id']);
        $response = $this->wework->getClient()->postJson('/cgi-bin/chatdata/close_debug_mode', $post);
        response(200, '操作成功', $response->toArray());
    }

    public function setReceiveCallback()
    {
        $post = $this->request->post(['program_id']);
        $response = $this->wework->getClient()->postJson('/cgi-bin/chatdata/set_receive_callback', $post);
        response(200, '操作成功', $response->toArray());
    }

    public function setPublicKey()
    {
        $post = $this->request->post(['public_key','public_key_ver']);
        $response = $this->wework->getClient()->postJson('/cgi-bin/chatdata/set_public_key', $post);
        response(200, '操作成功', $response->toArray());
    }
}